﻿import sys  
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QFileDialog  
from PyQt5.QtGui import QPixmap  
from PyQt5.QtCore import Qt  
  
class MyWidget(QWidget):  
    def __init__(self):  
        super().__init__()  
  
        # 创建图片标签  
        self.image_label1 = QLabel(self)  
        self.image_label2 = QLabel(self)  
  
        # 初始时不显示图片  
        self.image_label1.setPixmap(QPixmap())  
        self.image_label2.setPixmap(QPixmap())  
  
        # 创建垂直布局  
        vertical_layout1 = QVBoxLayout()  
        vertical_layout2 = QVBoxLayout()  
  
        # 将图片标签添加到垂直布局中  
        vertical_layout1.addWidget(self.image_label1)  
        vertical_layout2.addWidget(self.image_label2)  
  
        # 创建按钮  
        self.button_left = QPushButton('Open Image 1', self)  
        self.button_right = QPushButton('Open Image 2', self)  
  
        # 为按钮添加点击事件处理函数  
        self.button_left.clicked.connect(self.open_image_1)  
        self.button_right.clicked.connect(self.open_image_2)  
  
        # 创建水平布局  
        horizontal_layout = QHBoxLayout()  
  
        # 将组件添加到水平布局中  
        horizontal_layout.addWidget(self.button_left)  
        horizontal_layout.addStretch(1)  # 添加一个弹性空间以分隔按钮和图片  
        horizontal_layout.addLayout(vertical_layout1)  
        horizontal_layout.addLayout(vertical_layout2)  
        horizontal_layout.addStretch(1)  # 添加一个弹性空间以分隔图片和按钮  
        horizontal_layout.addWidget(self.button_right)  
  
        # 设置窗口的布局  
        self.setLayout(horizontal_layout)  
  
        # 设置窗口的标题和大小  
        self.setWindowTitle('Open Image Example')  
        self.setFixedSize(600, 400)  
  
    def open_image_1(self):  
        # 通过文件对话框选择图片  
        options = QFileDialog.Options()  
        options |= QFileDialog.DontUseNativeDialog  
        fileName, _ = QFileDialog.getOpenFileName(self, "Open Image", "",  
                                                 "All Files (*);;JPEG Files (*.jpg *.jpeg);;PNG Files (*.png)", options=options)  
        if fileName:  
            # 显示选中的图片  
            pixmap = QPixmap(fileName)  
            self.image_label1.setPixmap(pixmap.scaled(200, 200, Qt.KeepAspectRatio))  # 你可以根据需要调整大小  
  
    def open_image_2(self):  
        # 类似 open_image_1 方法，但更新第二个标签  
        options = QFileDialog.Options()  
        options |= QFileDialog.DontUseNativeDialog  
        fileName, _ = QFileDialog.getOpenFileName(self, "Open Image", "",  
                                                 "All Files (*);;JPEG Files (*.jpg *.jpeg);;PNG Files (*.png)", options=options)  
        if fileName:  
            pixmap = QPixmap(fileName)  
            self.image_label2.setPixmap(pixmap.scaled(200, 200, Qt.KeepAspectRatio))  
  
if __name__ == '__main__':  
    app = QApplication(sys.argv)  
    widget = MyWidget()  
    widget.show()  
    sys.exit(app.exec_())